<?php


namespace app\controller\api;

use app\common\model\SchoolModel;
use app\common\service\redis\PoolRedis;
use app\model\AdminAppRotation;
use app\model\AdminAppVersion;
use app\model\AdminAttendanceSys;
use app\model\AdminOption;
use app\model\AdminStaff;
use app\util\ReturnCode;
use think\facade\Log;
use think\Response;

class System extends Base
{
    public function info(): Response {
        $phone = AdminOption::where('name', 'service_phone')->where('group', 'default')->value('value');
        return $this->buildSuccess([
            'service_contant' => $phone
        ]);
    }

    /**
     * @notes: 返回当前设置的最新版本信息
     * @return Response
     * @author: speed
     */
    public function version(): Response {
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
            $isIos = true;
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
            $isIos = false;
        }else{
            $isIos = false;
        }
        $where = [
            ['is_top', '=', 1],
            ['is_delete', '=', 0]
        ];
        if ($isIos) {
            $where[] = ['ios', '=', 1];
        } else {
            $where[] = ['android', '=', 1];
        }
//        // 已登录按照学校来查找，未登录是一律返回无需升级
//        $uuid = input('get.uuid/s');
//        if ($uuid) {
//            $staff = AdminStaff::cache(true,120) // 缓存两分钟
//            ->where([
//                'is_delete' => 0,
//                'status' => AdminStaff::AUDIT_PASS,
//                'uuid' => $uuid,
//            ])->find();
//            if (!$staff) {
//                return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账号不存在或未审核通过');
//            }
//            if ($staff->type == 0 && $staff->expire_flag) {
//                return $this->buildFailed(ReturnCode::AUTH_ERROR, '账号已过期，无法登录');
//            }
//            $new = AdminAppVersion::where($where)
//                ->order(['is_top' => 'DESC', 'create_time' => 'DESC'])
//                ->where('school_id', '=', $staff->school_id)
//                ->field('version,ios,ios_link,android,android_link,force_update,content,create_time')
//                ->findOrEmpty();
//            if(!$new->isEmpty()){
//                return $this->buildSuccess([
//                    'latest' => $new->toArray()
//                ]);
//            }
//            // 找不到学校的按默认的来
//            $new = AdminAppVersion::where($where)
//                ->order(['is_top' => 'DESC', 'create_time' => 'DESC'])
//                ->whereNull('school_id')
//                ->field('version,ios,ios_link,android,android_link,force_update,content,create_time')
//                ->findOrEmpty();
//            return $this->buildSuccess([
//                'latest' => $new->isEmpty() ? [] : $new->toArray()
//            ]);
//        }
//        return $this->buildFailed(ReturnCode::DATA_EXISTS, '未登录返回最新版本信息');

        // 按默认的来，进入APP判断是否为最新版本，不管是否登录
        $new = AdminAppVersion::where($where)
            ->order(['is_top' => 'DESC', 'create_time' => 'DESC'])
            ->whereNull('school_id')
            ->field('version,ios,ios_link,android,android_link,force_update,content,create_time')
            ->findOrEmpty();
        return $this->buildSuccess([
            'latest' => $new->isEmpty() ? [] : $new->toArray()
        ]);
    }

    /**
     * @notes: 获取探测信息
     * @return Response
     */
    public function net_5g_info(): Response {
        $name = input('get.name/s');
        if ($name == 'gsta') {
            return $this->buildSuccess([
                'publicIp' => '183.56.201.235',
                'privateIp' => '192.168.10.1',
                'name' => 'gstalfc',
                'token' => 'NaDxK5033W2piPZRGeN5g_6deTAs@ncJP8IJl5'
            ]);
        }
        return $this->buildSuccess([
            'publicIp' => '114.114.114.114',
            'privateIp' => '192.168.11.1',
            'name' => 'test',
            'token' => 'NaDxK5033W2piPZRGeN'
        ]);
    }

    public function app_rotation(): Response {
        $school_id = input('get.school_id/s');

        $key = AdminAppRotation::APP_ROTATION_CACHE_KEY.$school_id;
        $redis = PoolRedis::instance();
        $res = $redis->get($key);
        if ($res) {
            return $this->buildSuccess($res);
        }
        $list = AdminAppRotation::where(['school_id' => $school_id])
            ->field('title,images')
            ->order('create_time', 'desc')
            ->select()->toArray();
        if ($list) {
            $redis->set($key, $list);
            return $this->buildSuccess($list);
        }

        $key = AdminAppRotation::APP_ROTATION_CACHE_KEY.'default';
        $redis = PoolRedis::instance();
        $res = $redis->get($key);
        if ($res) {
            return $this->buildSuccess($res);
        }
        $list = AdminAppRotation::whereNull('school_id')
            ->field('title,images')
            ->order('create_time', 'desc')
            ->select()->toArray();
        if ($list) {
            $redis->set($key, $list);
            return $this->buildSuccess($list);
        }

        return $this->buildSuccess([]);
    }

    public function home_info(): Response {
        $school_id = input('get.school_id/s');
        $uuid = input('get.uuid/s');
        $school = SchoolModel::getSchool($school_id);
        if (!$school) {
            return $this->buildSuccess([]);
        }
        $staff = AdminStaff::where([
            'is_delete' => 0,
            'status' => AdminStaff::AUDIT_PASS,
            'uuid' => $uuid,
        ])->find();
        if (!$staff) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账号不存在或未审核通过');
        }
        if ($staff->type == 0 && $staff->expire_flag) {
            return $this->buildFailed(ReturnCode::AUTH_ERROR, '账号已过期，无法登录');
        }
        if ($staff->type == 1 || $staff->user_level == 1) {
            return $this->buildSuccess([
                'home_menu' => $school->member_home_menu
            ]);
        } else {
            return $this->buildSuccess([
                'home_menu' => $school->general_home_menu
            ]);
        }
    }
}